package com.client.handler;

import com.common.message.vo.BaseMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientHandler extends ChannelInboundHandlerAdapter {

    private final static Logger logger = LoggerFactory.getLogger(ClientHandler.class);


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            logger.info("client read");
            if(msg != null && msg instanceof BaseMessage){
                BaseMessage baseMessage = (BaseMessage)msg;
                logger.info("client receive message {}:",baseMessage.toString());
            }
        }catch(Exception e){
            logger.error("[ERROR-CLIENT]:{}",e);
        } finally{
            ReferenceCountUtil.release(msg);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        logger.error("[ERROR-CLIENT]{}",cause);
    }
}
